package com.fenghuajueli.module_user.ui.helpcenter

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import coil.compose.AsyncImage
import com.blankj.utilcode.util.LogUtils
import com.fenghuajueli.libbasecoreui.baseswitch.SwitchKeyUtils
import com.fenghuajueli.module_user.R
import com.fenghuajueli.module_user.ui.theme.ModuleDevTheme
import com.fenghuajueli.module_user.ui.widget.HelpCenterBottomWidget
import com.fenghuajueli.module_user.ui.widget.UserTopBarWidget
import com.fenghuajueli.module_user.ui.widget.noRippleClickable
import com.kwad.sdk.core.b.a.it

class HelpCenterActivity : ComponentActivity() {
    companion object {
        @JvmStatic
        fun start(context: Context) {
            val starter = Intent(context, HelpCenterActivity::class.java)
            context.startActivity(starter)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            val viewModel = viewModel<HelpCenterViewModel> {
                HelpCenterViewModel(4)
            }

            val helpCenter = viewModel.helpCenterData.collectAsState()

            ModuleDevTheme {
                Surface(
                    modifier = Modifier.fillMaxSize()
                ) {
                    Box(
                        modifier = Modifier
                            .fillMaxWidth()
                            .background(color = MaterialTheme.colorScheme.secondary)
                    ) {
                        Column(
                            modifier = Modifier.fillMaxSize()
                        ) {
                            UserTopBarWidget(
                                primary = MaterialTheme.colorScheme.primary,
                                onPrimary = MaterialTheme.colorScheme.onPrimary,
                                title = "帮助中心",
                                topPadding = 0.dp,
                                onBackClick = ::finish
                            )

                            Column(
                                modifier = Modifier
                                    .weight(1f)
                                    .verticalScroll(rememberScrollState())
                            ) {
                                Spacer(modifier = Modifier.height(14.dp))

                                HelpCenterItem.data.forEach { item ->
                                    when {
                                        item.title == "使用教程" && helpCenter.value.data_list.isNotEmpty() -> HelpCenterItem(item)
                                        item.title == "会员问题" && SwitchKeyUtils.getPayStatus() -> HelpCenterItem(item)
                                        item.title == "功能异常" || item.title == "广告问题" -> HelpCenterItem(item)
                                    }
                                }
                            }

                            HelpCenterBottomWidget(modifier = Modifier)
                        }
                    }
                }

            }
        }
    }

    @Composable
    fun HelpCenterItem(
        helpCenterItem: HelpCenterItem
    ) {
        Row(
            modifier = Modifier
                .padding(horizontal = 16.dp, vertical = 6.dp)
                .fillMaxWidth()
                .background(
                    color = MaterialTheme.colorScheme.tertiary,
                    shape = RoundedCornerShape(8.dp)
                )
                .noRippleClickable {
                    HelpDetailActivity.start(this, helpCenterItem.title)
                }
                .padding(start = 15.dp, end = 12.dp, top = 20.dp, bottom = 20.dp),
            verticalAlignment = Alignment.CenterVertically
        ) {
            AsyncImage(
                modifier = Modifier
                    .size(38.dp),
                model = helpCenterItem.icon,
                contentDescription = null,
                contentScale = ContentScale.FillBounds
            )

            Spacer(modifier = Modifier.width(15.dp))

            Column {
                Text(
                    text = helpCenterItem.title,
                    fontSize = 15.sp
                )
                Text(
                    text = helpCenterItem.desc,
                    fontSize = 12.sp,
                    color = MaterialTheme.colorScheme.onSecondary
                )
            }

            Spacer(modifier = Modifier.weight(1f))

            AsyncImage(
                modifier = Modifier
                    .size(16.dp),
                model = R.mipmap.module_user_icon_more,
                contentDescription = null,
                contentScale = ContentScale.FillBounds
            )
        }
    }

    @Preview
    @Composable
    private fun HelpCenterItemP() {
        HelpCenterItem(HelpCenterItem.data.first())
    }
}